﻿
; (function (window, undefined, $) {
    var Request = new Object();
    Request = GetRequest();
    $(document).ready(function () {
        var gridopt = {
            url: options.listUrl + '?id=' + $('#id').val(),
            colModel: [
                    { display: '还款编号', name: 'RepaymentPlanNO', width: "20%", sortable: true, hide: false, align: 'left', iskey: true },
                    { display: '还款日期', name: 'RepaymentPlanDate', width: "15%", sortable: false, align: 'left' },
                    { display: '当期本息', name: 'CurrentPrincipal', width: "15%", sortable: false, align: 'left' },
                    { display: '剩余本金', name: 'RemainingPrincipal', width: "20%", sortable: false, align: 'left' },
                    { display: '还款状态', name: 'status', width: "15%", sortable: false, align: 'left' }
            ],
            sortname: "ID",
            sortorder: "ASC",
            title: false,
            rp: 5,
            usepager: true,
            showcheckbox: false
        };
        var xjgrid = new xjGrid("gridlist", gridopt);

        var gridrepaymentopt = {
            url: options.RepaymentlistUrl + "?projectid=" + Request["id"],// + "&id=" + Request["pid"],
            colModel: [
                    { display: '项目编号', name: 'Id', width: "5%", sortable: true, hide: false, align: 'left', iskey: true },
                    //{ display: 'pid', name: 'pid', hide: true, width: "15%", sortable: false, align: 'left' },
                    //{ display: '项目名称', name: 'ProjectName', width: "15%", sortable: false, align: 'left' },
                    { display: '投资人', name: 'RealName', width: "15%", sortable: false, align: 'left' },
                    { display: '投资金额', name: 'TenderAmt', width: "15%", sortable: false, align: 'left', process: processTotalLoan },
                    //{ display: '还款编号', name: 'RepaymentPlanNO', width: "10%", sortable: false, align: 'left' },
                    //{ display: '还款日期', name: 'RepaymentPlanDate', width: "12%", sortable: false, align: 'left' },
                    //, , 
                    { display: '年化收益率', name: 'AnnualisedRates', width: "12%", sortable: false, align: 'left', process: processAnnualisedRates },
                    { display: '活动受增', name: 'AddAnnualisedRates', width: "12%", sortable: false, align: 'left', process: processAnnualisedRates },
                    { display: '会员受增', name: 'AddMAnnualisedRates', width: "12%", sortable: false, align: 'left', process: processAnnualisedRates },

                    { display: '当期收益', name: 'Profit', width: "12%", sortable: false, align: 'left', process: processTotalLoan }//,
                    //{ display: '剩余本金', name: 'RemainingPrincipal', width: "13%", sortable: false, align: 'left', process: processTotalLoan },
                    //{ display: '投资金额', name: 'TenderAmt', width: "8%", sortable: false, align: 'left' }//,
                    //{ display: '操作', name: 'ID', width: "10%", sortable: false, align: 'center', process: processOp }
            ],
            sortname: "ID",
            sortorder: "ASC",
            title: false,
            rp: 5,
            usepager: true,
            showcheckbox: false
        };
        var xjgrid = new xjGrid("gridrepaymentlist", gridrepaymentopt);
    })


    var submiting = false;
    $('#frmEdit').validator({
        rules: {
            // 自定义规则，如果通过返回true，否则返回错误消息
            //cusidcard: function (element, param, field) {
            //    var idtype = $("#Id").val();
            //    if (idtype == param[0]) {
            //        return /^.+$/.test($(element).val()) || "必填项";
            //    }
            //    return true;
            //}
        },
        fields: {
            '#ProjectNo': 'required',
            '#ProjectName': 'required',
            '#ProjectContent': 'required',
            '#TotalLoan': 'required',
            '#AnnualisedRates': 'required',
            '#EnterpriseID': 'required',
            '#EnterpriseAgent': 'required',
            '#ProjectLocation': 'required',
            '#RepaymentPlanDate': 'required',
            '#BiddingDeadline': 'required',
            '#LoanPurpose': 'required',
            '#RepaymentSource': 'required',
            '#LoanType': 'required',
            '#AuditStatus': 'required',
            '#LoanTime': 'required',
            '#ProjectDuration': 'required'
        },
        valid: function (form) {
            if (submiting) {
                return;
            }
            submiting = true;
            FormSubmit(form, function (res) {
                if (res.Status > 0) {
                    alert("操作成功！");
                    window.location.href = "/Project/Project.html";

                }
                else {
                    alert(res.Message);
                }
                submiting = false;
            })
        }
    });

    $("#btnReturn").click(function (e) {
        window.location.href = "/Project/Project.html";
    });
    $("#btnClose").click(function (e) {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    });

    $("#btnLoan").click(function (e) {
        if ($('#LoanUser').val() != "") {
            $.post(options.Loan , { id: $('#id').val(), name: $('#LoanUser').val() },
              function (res) {
                  if (res > 0) {
                      alert("操作成功！");
                      window.location.href = "/Project/Project.html";
                  }
                  else {
                      alert("操作失败！");
                  }
              },
              "json"
        );
        }
        else {
            alert('借款人不能为空！');
        }
    });
    
    //审核通过
    $("#btnAudit").click(function (e) {
            $.post(options.Audit, { id: $('#id').val(), name: $('#AuditUser').val(), status: 2, Audit: $('#AuditExplain').val() },
              function (res) {
                  if (res > 0) {
                      alert("操作成功！");
                      window.location.href = "/Project/Project.html";
                  }
                  else {
                      alert("操作失败！");
                  }
              },
              "json"
        );
        
    });

    //审核不通过
    $("#btnBack").click(function (e) {
            $.post(options.Audit, { id: $('#id').val(), name: $('#AuditUser').val(), status: 1, Audit: $('#AuditExplain').val() },
              function (res) {
                  if (res > 0) {
                      alert("操作成功！");
                      window.location.href = "/Project/Project.html";
                  }
                  else {
                      alert("操作失败！");
                  }
              },
              "json"
        );
    });


    $("#btnRept").click(function (e) {
            if ($('#RepaymentUser').val() != "") {
                $.post(options.Rept, { id: $('#id').val(), name: $('#RepaymentUser').val() },
                  function (res) {
                      if (res > 0) {
                          alert("操作成功！");
                          window.location.href = "/Project/Project.html";
                      }
                      else {
                          alert("操作失败！");
                      }
                  },
                  "json"
            );
            }
            else {
                alert('还款人不能为空！');
            }
    });
    
    util.GetEnterpriseName = function (id) {
        $.layer({
            type: 2,
            shade: [0],
            fix: false,
            title: '企业信息',
            maxmin: false,
            iframe: { src: options.enterpriseUrl },
            area: ['800px', '440px'],
            close: function (index) {
                //子窗口返回值 layer.getChildFrame('#name', index).val()
                //layer.msg('您获得了子窗口标记：' + layer.getChildFrame('#name', index).val(), 3, 1);
            },
            end: function (index) {
                if ($('#hideEName').val()!="") {
                    $('#lbEnterpriseName').html($('#hideEName').val());
                }
            }
        });

    };
    util.GetRates = function (id,num) {
        num = num.replace(/[^0-9|\.]/g, '');
        if (num>1) {
            num = 1;
        }
        $('#' + id).val(num);
    }

    util.GetNum = function (id, num) {
        num = num.replace(/[^0-9|\.]/g, ''); //清除字符串中的非数字非.字符
        if (num > 100) {
            num = 100;
        }
        var no = changeTwoDecimal(parseFloat(num));
        if (!no) {
            $('#' + id).val('');
        }
        else {
            $('#' + id).val(no.toFixed(2));
        }
    }

    util.GetNums = function (id, num) {
        num = num.replace(/[^0-9|\.]/g, ''); //清除字符串中的非数字非.字符
        $('#' + id).val(num);
    }

    util.getFloatStr = function (id, num) {
        num = num.replace(/[^0-9|\.]/g, ''); //清除字符串中的非数字非.字符
        var no = changeTwoDecimal(parseFloat(num));
        if (!no) {
            $('#' + id).val('');
        }
        else {
            $('#' + id).val(no.toFixed(2));
        }
    };

    function changeTwoDecimal(x) {
        if (isNaN(x)) {
            return false;
        }
        f_x = Math.round(x * 100) / 100;
        return f_x;
    }

    util.Delete = function (id) {
        if (!confirm("您确定要执行删除操作吗？")) {
            return false;
        }
        $.post(options.deleteUrl + "/" + id, { id: id },
              function (res) {
                  if (res.Status > 0) {
                      window.location.reload();
                  }
                  else {
                      alert("操作失败！");
                  }
              },
              "json"
        );
    };

    function processAnnualisedRates(value) {
        var ops = [];
        ops.push(Math.round(value * 100), "%");
        return ops.join("");
    }

    function processTotalLoan(value) {
        return formatNum(value);
    }

    function formatNum(v) {
        if (isNaN(v)) {
            return v;
        }
        v = (Math.round((v - 0) * 100)) / 100;
        v = (v == Math.floor(v)) ? v + ".00" : ((v * 10 == Math.floor(v * 10)) ? v
                + "0" : v);
        v = String(v);
        var ps = v.split('.');
        var whole = ps[0];
        var sub = ps[1] ? '.' + ps[1] : '.00';
        var r = /(\d+)(\d{3})/;
        while (r.test(whole)) {
            whole = whole.replace(r, '$1' + ',' + '$2');
        }
        v = whole + sub;

        return v;
    }

})(window, undefined, jQuery);

$(document).ready(function () {
    var Request = new Object();
    Request = GetRequest();
    if (Request["status"]!=null) {
        if (Request["status"]==1) {//添加项目
            $('#btnAudit').css('display', 'none');
            $('#RepaymentDetailTitle').css('display', 'none');
            $('#RepaymentDetail').css('display', 'none');
        }
        if (Request["status"] == 2) {//审核项目
            $('#btnSave').css('display', 'none');
            $('#RepaymentDetailTitle').css('display', 'none');
            $('#RepaymentDetail').css('display', 'none');
        }
        if (Request["status"] == 3) {//查看项目
            $('#btnAudit').css('display', 'none');
            $('#btnSave').css('display', 'none');

        }
    }
})

function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}